package com.dwjm.exam.stu_list;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/deleteMultipleStudents")
public class DelMultipleStudentsHandler extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        
        // Get comma-separated student IDs
        String studentIdsParam = req.getParameter("studentIds");
        
        if (studentIdsParam == null || studentIdsParam.trim().isEmpty()) {
            // No student IDs provided
            String jsonResponse = "{\"success\":false,\"message\":\"未选择任何学生\"}";
            resp.getWriter().write(jsonResponse);
            return;
        }
        
        // Split the comma-separated IDs into an array
        String[] studentIds = studentIdsParam.split(",");
        
        StudentDaoImpl studentDao = new StudentDaoImpl();
        int deletedCount = studentDao.deleteMultipleStudents(studentIds);
        
        if (deletedCount > 0) {
            // Return JSON response
            String jsonResponse = "{\"success\":true,\"message\":\"成功删除 " + deletedCount + " 名学生\"}";
            resp.getWriter().write(jsonResponse);
        } else {
            String jsonResponse = "{\"success\":false,\"message\":\"删除失败，请稍后重试\"}";
            resp.getWriter().write(jsonResponse);
        }
    }
} 